<template>
  <div class="main">
    <div class="signup" >
      <div class="text">
      欢迎注册智慧医疗
    </div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="m-ruleform">
        <div style="display: flex;flex: 1;margin-right: 15px">
          <el-form-item label="用户名" prop="name" >
            <el-input v-model="ruleForm.uname"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" style="margin-right: 25px">
            <el-input v-model="ruleForm.pwd"></el-input>
          </el-form-item>
          <el-form-item label="真实姓名" prop="realname">
            <el-input v-model="ruleForm.realname"></el-input>
          </el-form-item>
        </div>
        <div style="display: flex;flex: 1">
          <el-form-item label="电话号码" prop="phonenumber">
            <el-input v-model="ruleForm.phonenumber"></el-input>
          </el-form-item>
          <el-form-item label="用户类型" prop="utype">
            <el-radio v-model="ruleForm.utype" label="ROLE_1" disabled>普通用户</el-radio>
          </el-form-item>
        </div>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>


        <div class="sign">已有账号？点击<span style="color: blue;cursor: pointer;" @click="$router.push('/login')">登录</span></div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {usersignup} from '../api/user'
import router from "@/router";
export default {
  data() {
    return {
      ruleForm: {
        realname: '',
        uname: '',
        pwd: '',
        phonenumber: '',
        utype: 'ROLE_1'
      },
      rules: {
        uname: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        realname: [
          { required: true, message: '请输入真实姓名', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入电话号码', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          usersignup(this.ruleForm).then(res=>{
            if(res.code==200){
              //注册成功
              alert("注册成功！")
              this.$router.push("/login")
            }else{
              //注册失败
              alert(res.msg)
            }
          }).catch(error=>{})
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

.signup{
  height: 50vh;
  width: 50%;
  background-color: aliceblue;
  border-radius: 5px;
  box-shadow: 2px 0 6px rgba(0,21,41,.35);
}
.main{
  overflow: hidden;
  height: 97vh;
  display: flex;
  align-items: center;
  background-color: #2abeb2;
  justify-content: center;
}

.text{
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  margin-top: 60px;
  margin-bottom: 50px;
  display: flex;
  justify-content: center;
}
.sign{
  display: flex;
  text-align: center;
  margin-left: 290px;
}
.m-ruleform{

}

</style>
